package baiqitun.stupro.project.core.task.impl;

import baiqitun.stupro.common.exception.enums.support.StuproExceptionEnum;
import baiqitun.stupro.common.exception.enums.support.NotFoundExceptionEnum;
import baiqitun.stupro.common.exception.support.NotFoundException;
import baiqitun.stupro.common.exception.StuproException;
import baiqitun.stupro.common.workflow.task.AbstractBooleanServiceTaskHandler;
import baiqitun.stupro.project.constant.SysProjectRoleEnum;
import baiqitun.stupro.project.core.news.SysProjectNews;
import baiqitun.stupro.project.core.news.constant.SysProjectNewsObjectTypeEnum;
import baiqitun.stupro.project.core.news.service.SysProjectNewsService;
import baiqitun.stupro.project.service.SysProjectService;
import baiqitun.stupro.project.service.SysProjectUserRoleService;
import baiqitun.stupro.security.entity.SysUser;
import baiqitun.stupro.security.service.SysUserService;
import org.apache.commons.lang.StringUtils;
import org.flowable.engine.delegate.DelegateExecution;
import org.flowable.task.api.history.HistoricTaskInstance;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 邀请用户加入项目流程的ServiceTask处理器
 * @author kedaji
 */
@Service("inviteUserToProjectServiceTaskHandler")
public class InviteUserToProjectServiceTaskHandler extends AbstractBooleanServiceTaskHandler {
    @Autowired
    private SysUserService sysUserService;
    @Autowired
    private SysProjectUserRoleService sysProjectUserRoleService;
    @Autowired
    private SysProjectService sysProjectService;
    @Autowired
    private SysProjectNewsService sysProjectNewsService;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void handleTrue(DelegateExecution execution) {
        // 邀请同意后将用户加入到此项目中
        HistoricTaskInstance historicTaskInstance = getHistoricTaskInstance(execution);
        String assignee = historicTaskInstance.getAssignee();
        String owner = historicTaskInstance.getOwner();
        String businessKey = execution.getProcessInstanceBusinessKey();
        if (businessKey == null){
            throw new StuproException(StuproExceptionEnum.ILLEGAL_PARAMETER).put(HistoricTaskInstance.class, "businessKey", null);
        }
        sysProjectService.checkNotFound(businessKey, new NotFoundException(NotFoundExceptionEnum.USER_NOT_FOUND));
        sysProjectUserRoleService.saveUserToProject(businessKey, assignee, historicTaskInstance.getOwner());

        // 保存一条项目动态
        SysProjectNews projectNews = new SysProjectNews();
        SysUser user = sysUserService.getEnabledUser(owner);
        if (StringUtils.isBlank(user.getRealName())){
            projectNews.setRealName(user.getUsername());
        }
        projectNews.setUserId(assignee).setObjectType(SysProjectNewsObjectTypeEnum.USER.name()).setUserId(assignee).setCreateBy(owner);
        sysProjectNewsService.saveCreateTypeNews(projectNews);
        // TODO 发消息给任务发起人
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void handleFalse(DelegateExecution execution) {
        // TODO 发消息给任务发起人
    }
}
